package org.hxyjs.dao.impl;

import org.hxyjs.dao.EventDetailsDao;
import org.hxyjs.entity.EventDetails;
import org.hxyjs.mapper.EventDetailsMapper;
import org.hxyjs.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository("eventdetailsDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class EventDetailsDaoImpl implements EventDetailsDao{
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private EventDetailsMapper eventdetailsMapper;

    public List<EventDetails>  getAllEventDetailss() throws Exception{ return eventdetailsMapper.selectByExample(null); }

    @Override
    public EventDetails getEventDetailsByUno(String uno) throws Exception {
        return eventdetailsMapper.selectByPrimaryKey(uno); }

    @Override
    public List<EventDetails> getEventDetailssByPage(PageInfo pageInfo) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount =(int) eventdetailsMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return eventdetailsMapper.getEventDetailsByPage(map);
    }

    @Override
    public Integer deleteEventDetails(String uno) { return eventdetailsMapper.deleteByPrimaryKey(uno); }

    @Override
    public Integer updateEventDetails(EventDetails eventdetails) { return eventdetailsMapper.updateByPrimaryKey(eventdetails); }



    @Override
    public EventDetails getEventDetailssWithOrders(String uno) throws Exception {
        return null;
    }

    @Override
    public EventDetails getAccountByAccountName(String uno) throws Exception {
        return null;
    }

    @Override
    public Integer addEventDetailsByUno(EventDetails eventdetails){
        eventdetails.setEdno(initId());
        System.out.println(eventdetails);
        return eventdetailsMapper.insertSelective(eventdetails);
    }
//
//    @Override
//    public EventDetails getEventDetailssWithOrders(String uno) throws Exception {
//        return eventdetailsMapper.getEventDetailssWithOrders(uno);
//    }
//    @Override
//    public EventDetails getAccountByAccountName(String uname) throws Exception {
//        return eventdetailsMapper.getAccountByAccountName(uname);
//    }
//
//
    public String initId() {
        String maxCano = eventdetailsMapper.getMaxEdno();
        maxCano = Optional.ofNullable(maxCano).orElse("0000"); //如果maxUno 为空 就为0000
        return org.hxyjs.utils.GenNumTools.initId("e", 4, maxCano);
    }
}
